/*
 * Copyright 2011-2020 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0.
 * See `LICENSE` in the project root for license information.
 */

package me.ijleex.mgmt.codegen.boot.autoconfigure;

import java.util.Properties;

import org.apache.velocity.app.Velocity;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;

/**
 * 代码生成相关配置
 *
 * @author liym
 * @since 2019-02-28 13:58 新建
 */
@EnableConfigurationProperties(CodeGenProperties.class)
@Configuration
public class CodeGenAutoConfiguration implements InitializingBean {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    private final CodeGenProperties properties;

    public CodeGenAutoConfiguration(CodeGenProperties properties) {
        this.properties = properties;

        this.init();
    }

    @Override
    public void afterPropertiesSet() {
        CodeGenProperties.CodeGen codeGen = this.properties.getCodeGen();
        String basicPackage = codeGen.getBasicPackage();
        System.out.println("basicPackage: " + basicPackage);
    }

    /**
     * 初始化 Velocity 引擎
     *
     * @see org.apache.velocity.runtime.RuntimeConstants#INPUT_ENCODING
     */
    private void init() {
        Properties p = new Properties();
        try {
            // 加载 velocity.properties 文件
            p.load(this.getClass().getResourceAsStream("/velocity.properties"));

            Velocity.init(p);
            this.logger.debug("Velocity engine initialized!");
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    // public static void main(String[] args) {
    //     VelocityInitializer.init();
    //
    //     Template template = Velocity.getTemplate("templates/sql/menu.sql.vm");
    //     System.out.println(template);
    // }
}
